package com.yiyi.credit.manage.abutment.service;


import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yiyi.credit.manage.abutment.common.enums.TdInterfacePathEnum;
import com.yiyi.credit.manage.abutment.common.enums.TdReturnCodeEnum;
import com.yiyi.credit.manage.abutment.common.enums.platFormEnum;
import com.yiyi.credit.manage.abutment.common.utils.DateUtil;
import com.yiyi.credit.manage.abutment.common.utils.UUIDUtil;
import com.yiyi.credit.manage.abutment.common.utils.WebUtil;
import com.yiyi.credit.manage.abutment.mapper.TdMapper;
import com.yiyi.credit.manage.model.entity.InterfaceFlow;
import com.yiyi.credit.manage.model.entity.InterfaceFlowTd;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TdService {

    public static final Logger LOGGER = LoggerFactory.getLogger(TdService.class);

    @Autowired
    private TdMapper tdMapper;

    @Value("${td.url}")
    private String tdurl;

    @Value("${td.partner_code}")
    private String partnerCode;

    @Value("${td.partner_key}")
    private String partnerKey;

    @Value("${td.app_name}")
    private String appName;


    public List<InterfaceFlowTd> uniqueRequestNo(String projectCode, String requestNo){
        List<InterfaceFlowTd> list=tdMapper.selectInterfaceFlowTdByRequestNo(projectCode,requestNo);
        return list;
    }

    public Map<String, Object> antifraud(String reqDate, String appid, String requestNo) throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        JSONObject reqJson = JSONObject.parseObject(reqDate);
        String appId = reqJson.get("appId").toString();
        reqJson.remove("appId");
        reqJson.remove("sign");
        reqJson.remove("requestNo");

        String id = UUIDUtil.generateOrdId();
        Date date = new Date();
        String createTime = DateUtil.format(date);
        InterfaceFlowTd interfaceFlowTd = new InterfaceFlowTd();
        interfaceFlowTd.setId(id);
        interfaceFlowTd.setCreateTime(date);
        interfaceFlowTd.setProjectCode(appid);
        interfaceFlowTd.setPlatformCode(partnerCode);
        interfaceFlowTd.setInterfaceType(TdInterfacePathEnum.ANTIFRAUD.getPath());
        interfaceFlowTd.setAccount(appName);
        interfaceFlowTd.setRequestNo(requestNo);
        interfaceFlowTd.setProjectReqDate(reqDate);
        interfaceFlowTd.setReqData(reqJson.toString());
        int insertState = tdMapper.insert(interfaceFlowTd);


        TreeMap<String, String> params = JSONObject.parseObject(reqJson.toJSONString(), new TypeReference<TreeMap<String, String>>(){});
        String antifraudUrl = tdurl  +"?partner_code=" + partnerCode + "&partner_key=" + partnerKey + "&app_name=" + appName;
        String respStr= WebUtil.doPost(antifraudUrl,params);

        JSONObject respObjcect = JSONObject.parseObject(respStr);
        String code = null;
        String msg = null;
        if(respObjcect.get("reason_code") != null)
            code = respObjcect.get("reason_code").toString();
        if(respObjcect.get("reason_desc") != null)
            msg =  respObjcect.get("reason_desc").toString();;
        if (StringUtils.isEmpty(code)){
            code = TdReturnCodeEnum.CODE0000.getCode();
            msg = TdReturnCodeEnum.CODE0000.getValue();
        }

        JSONObject data = null;
        if(respObjcect.get("result_desc")!=null) data = (JSONObject)respObjcect.get("result_desc");

        InterfaceFlowTd interfaceFlowUpdate = new InterfaceFlowTd();
        interfaceFlowUpdate.setId(id);
        interfaceFlowUpdate.setRespData(respStr);
        interfaceFlowUpdate.setUpdateTime(new Date());
        interfaceFlowUpdate.setResCode(code);
        interfaceFlowUpdate.setResMsg(msg);
        int updateState =tdMapper.updateByPrimaryKeySelective(interfaceFlowUpdate);

        map.put("respStr",respStr);
        map.put("code",code);
        map.put("msg",msg);
        map.put("data",data);
        map.put("requestNo",requestNo);

        return  map;
    }
}

